package com.zrar.ak.gkpt.controller.manage;

import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.manage.IManageRoleInfoService;
import com.zrar.ak.gkpt.service.user.IUserService;
import com.zrar.ak.gkpt.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: ManageRoleController
 * @Description 角色管理Controller层
 * @Author liyang
 * @Time 2020/03/24
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理相关接口")
@Slf4j
public class ManageRoleInfoController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IManageRoleInfoService roleManageService;


    @RequestMapping(value = "cudRoleInfo", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "增改删角色信息", httpMethod = "POST", notes = "增改删角色信息")
    public JsonResult cudRoleInfo(HttpServletRequest request,
                                             @ApiParam(name = "formDataParameter", value = "增改删角色信息-角色操作数据参数") @RequestBody FormDataParameter formDataParameter){
        JsonResult jsonResult = new JsonResult();
        String formDataJson = formDataParameter.getFormDataJson();
        UserVO userVO = userService.getUserInformation(request);
        try {
            jsonResult = roleManageService.cudRoleInfo(jsonResult, formDataJson, userVO);
        } catch (Exception e) {
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @RequestMapping(value = "getRoleInfoList", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取角色信息列表-分页查询和条件查询", httpMethod = "GET", notes = "获取角色信息列表-分页查询和条件查询")
    public JsonPageResult getRoleInfoList(HttpServletRequest request,
                                                    @ApiParam(name = "currentPage", value = "当前页") @RequestParam int currentPage,
                                                    @ApiParam(name = "pageSize", value = "每页条数限制") @RequestParam int pageSize,
                                                    @ApiParam(name = "roleName", value = "角色名称(模糊查询)") @RequestParam(value = "roleName",required=false) String roleName){
        JsonPageResult jsonResult = new JsonPageResult();
        jsonResult = roleManageService.getRoleInfoList(jsonResult, currentPage,pageSize,roleName);
        return jsonResult;
    }



    @RequestMapping(value = "getRoleFieldInfoListByRoleId", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "通过角色id获取角色数据库表字段关联关系列表", httpMethod = "GET", notes = "通过角色id获取角色数据库表字段关联关系列表")
    public JsonResult getRoleFieldInfoListByRoleId(HttpServletRequest request,
                                                   @ApiParam(name = "roleId", value = "角色id") @RequestParam String roleId){
        JsonResult jsonResult = new JsonResult();
        jsonResult = roleManageService.getRoleFieldInfoListByRoleId(jsonResult, roleId);
        return jsonResult;
    }

    @RequestMapping(value = "updateRoleFieldInfo", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "修改角色的数据库表字段的权限信息", httpMethod = "POST", notes = "修改角色的数据库表字段的权限信息")
    public JsonResult updateRoleFieldInfo(HttpServletRequest request,
                                  @ApiParam(name = "formDataParameter", value = "角色的数据库表字段的权限信息") @RequestBody FormDataParameter formDataParameter){
        JsonResult jsonResult = new JsonResult();
        String formDataJson = formDataParameter.getFormDataJson();
        String roleId = formDataParameter.getId();
        UserVO userVO = userService.getUserInformation(request);
        try {
            jsonResult = roleManageService.updateRoleFieldInfo(jsonResult, formDataJson, roleId, userVO);
        } catch (Exception e) {
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            e.printStackTrace();
        }
        return jsonResult;
    }


}
